package com.ziyear.common.util;

import com.ziyear.common.constant.TrainConstant;
import com.ziyear.common.enums.SeatType;
import com.ziyear.common.enums.SeatUse;
import com.ziyear.domain.Train;
import com.ziyear.domain.TrainSeat;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述 : 火车工具类
 *
 * @author Ziyear
 */
public class TrainUtils {
    private static final Map<SeatType, Integer> carSeatNumMapping = new HashMap<>();

    private static final Map<SeatType, String> carSeatCodeMapping = new HashMap<>();

    private static final Map<SeatType, Integer> carSeatRowMapping = new HashMap<>();

    static {
        carSeatNumMapping.put(SeatType.SHANGWU, TrainConstant.SHANGWU_CAR_SEAT_NUM);
        carSeatNumMapping.put(SeatType.YIDENG, TrainConstant.YIDENG_CAR_SEAT_NUM);
        carSeatNumMapping.put(SeatType.ERDENG, TrainConstant.ERDENG_CAR_SEAT_NUM);
        carSeatCodeMapping.put(SeatType.SHANGWU, TrainConstant.SHANGWU_SEATS);
        carSeatCodeMapping.put(SeatType.YIDENG, TrainConstant.YIDENG_SEATS);
        carSeatCodeMapping.put(SeatType.ERDENG, TrainConstant.ERDENG_SEATS);
        carSeatRowMapping.put(SeatType.SHANGWU, TrainConstant.SHANGWU_CAR_ROW_NUM);
        carSeatRowMapping.put(SeatType.YIDENG, TrainConstant.YIDENG_CAR_ROW_NUM);
        carSeatRowMapping.put(SeatType.ERDENG, TrainConstant.ERDENG_CAR_ROW_NUM);
    }

    public static List<TrainSeat> buildTrainSeat(Train train) {
        Integer shangWuNum = train.getShangWuNum();
        Integer yiDengNumber = train.getYiDengNum();
        Integer erDengNumber = train.getErDengNum();
        int total = shangWuNum + yiDengNumber + erDengNumber;
        List<TrainSeat> result = new ArrayList<>();
        for (int i = 1; i <= total; i++) {
            if (i <= shangWuNum) {
                result.addAll(buildSeats(SeatType.SHANGWU, train.getTrainId(), i));
            } else if (i <= shangWuNum + yiDengNumber) {
                result.addAll(buildSeats(SeatType.YIDENG, train.getTrainId(), i));
            } else {
                result.addAll(buildSeats(SeatType.ERDENG, train.getTrainId(), i));
            }
        }
        return result;
    }

    private static List<TrainSeat> buildSeats(SeatType carsType, Integer trainId, Integer carNum) {
        List<TrainSeat> result = new ArrayList<>();
        for (int i = 1; i <= carSeatRowMapping.get(carsType); i++) {
            String codes = carSeatCodeMapping.get(carsType);
            String[] split = codes.split(TrainConstant.SPLIT);
            for (String code : split) {
                result.add(new TrainSeat(trainId, carNum, carsType.getCode(), code, i + code, SeatUse.UNUSED.name()));
            }
        }
        return result;
    }

    public static BigDecimal getPriceBySeatType(Train train, String seatType) {
        BigDecimal price = null;
        if (SeatType.SHANGWU.codeEquals(seatType)) {
            price = train.getShangWuPrice();
        } else if (SeatType.YIDENG.codeEquals(seatType)) {
            price = train.getYiDengPrice();
        } else if (SeatType.ERDENG.codeEquals(seatType)) {
            price = train.getErDengPrice();
        } else {
            throw new RuntimeException("错误的座位类型");
        }
        return price;
    }

    public static Integer getNumberBySeatType(Train train, String seatType) {
        Integer number = null;
        if (SeatType.SHANGWU.codeEquals(seatType)) {
            number = train.getShangWu();
        } else if (SeatType.YIDENG.codeEquals(seatType)) {
            number = train.getYiDeng();
        } else if (SeatType.ERDENG.codeEquals(seatType)) {
            number = train.getErDeng();
        } else {
            throw new RuntimeException("错误的座位类型");
        }
        return number;
    }

    // 减少
    public static void increaseNumber(Train train, String seatType) {
        if (SeatType.SHANGWU.codeEquals(seatType)) {
            train.setShangWu(train.getShangWu() + 1);
        } else if (SeatType.YIDENG.codeEquals(seatType)) {
            train.setYiDeng(train.getYiDeng() + 1);
        } else if (SeatType.ERDENG.codeEquals(seatType)) {
            train.setErDeng(train.getErDeng() + 1);
        } else {
            throw new RuntimeException("错误的座位类型");
        }
    }

    public static void reduceNumber(Train train, String seatType) {
        if (SeatType.SHANGWU.codeEquals(seatType)) {
            if (train.getShangWu() <= 0) {
                throw new RuntimeException("座位已售空");
            }
            train.setShangWu(train.getShangWu() - 1);
        } else if (SeatType.YIDENG.codeEquals(seatType)) {
            if (train.getYiDeng() <= 0) {
                throw new RuntimeException("座位已售空");
            }
            train.setYiDeng(train.getYiDeng() - 1);
        } else if (SeatType.ERDENG.codeEquals(seatType)) {
            if (train.getErDeng() <= 0) {
                throw new RuntimeException("座位已售空");
            }
            train.setErDeng(train.getErDeng() - 1);
        } else {
            throw new RuntimeException("错误的座位类型");
        }
    }

    public static String buildSeatCodeRadio(String seatType) {
        if (SeatType.SHANGWU.codeEquals(seatType)) {
            return "<td>" +
                    "<label for=\"seatCode\">A</label><input name=\"seatCode\" value=\"A\" type=\"radio\">&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "<label for=\"seatCode\">C</label><input name=\"seatCode\" value=\"C\" type=\"radio\">&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "<label for=\"seatCode\">F</label><input name=\"seatCode\" value=\"F\" type=\"radio\">&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "</td>";
        } else if (SeatType.YIDENG.codeEquals(seatType)) {
            return "<td>" +
                    "<label for=\"seatCode\">A</label><input name=\"seatCode\" value=\"A\" type=\"radio\">&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "<label for=\"seatCode\">C</label><input name=\"seatCode\" value=\"C\" type=\"radio\">&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "<label for=\"seatCode\">D</label><input name=\"seatCode\" value=\"D\" type=\"radio\">&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "<label for=\"seatCode\">F</label><input name=\"seatCode\" value=\"F\" type=\"radio\">&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "</td>";
        } else if (SeatType.ERDENG.codeEquals(seatType)) {
            return "<td>" +
                    "<label for=\"seatCode\">A</label><input name=\"seatCode\" value=\"A\" type=\"radio\">&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "<label for=\"seatCode\">B</label><input name=\"seatCode\" value=\"B\" type=\"radio\">&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "<label for=\"seatCode\">C</label><input name=\"seatCode\" value=\"C\" type=\"radio\">&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "<label for=\"seatCode\">D</label><input name=\"seatCode\" value=\"D\" type=\"radio\">&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "<label for=\"seatCode\">F</label><input name=\"seatCode\" value=\"F\" type=\"radio\">&nbsp;&nbsp;&nbsp;&nbsp;" +
                    "</td>";
        }
        throw new RuntimeException("错误的座位类型");
    }
}
